<?php


namespace App\Utils;


use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Utils;
use Psr\Http\Message\ResponseInterface;

class SyncRequest
{
    public  $requestClient = null;
    public  $requestPromise = null;
    public  $baseUrl = '';
    public  $requestMethod = 'GET';
    public  $requestResult = array('code' => 200,'message' => '');

    /**
     * SyncRequest constructor
     * @param string $baseUrl
     */
    public function __construct($baseUrl = '')
    {
        $this->setBaseUrl($baseUrl);
        $this->setClient();
    }

    /**
     * @param string $baseUrl
     */
    public function setBaseUrl($baseUrl = '')
    {
        $this->baseUrl = $baseUrl ? $baseUrl : config('third_party_api.oil_prices.url');
    }

    /**
     * 设置请求客户端
     */
    public function setClient()
    {
        $this->requestClient = new Client(array('base_uri' => $this->baseUrl));
    }
    /**
     * @param $method
     */
    public function setMethod($method)
    {
        if($method){
            $this->requestMethod = $method;
        }
    }
    /**
     * @param $query
     * @return \GuzzleHttp\Promise\PromiseInterface
     */
    public function syncBulkRequest($query)
    {
        $requests = function ($query){
            array_walk($query,function ($value){
                yield new Request($this->requestMethod, $this->baseUrl.'?'.http_build_query($value));
            });
        };
        $pool = new Pool($this->requestClient, $requests($query), [
            'concurrency' => 5,
            'fulfilled' => function (Response $response, $index){
                $this->requestResult[$index] = array();
                $contents = $response->getBody()->getContents();
                $contents_temp = Utils::jsonDecode($contents, true);
                $this->requestResult['data'][$index] = $contents_temp;
            },
            'rejected' => function (RequestException $reason, $index) {
                $this->requestResult['code'][$index] = $reason->getCode();
                $this->requestResult['message'][$index] = $reason->getMessage();
            },
        ]);
        $promise = $pool->promise();
        return $promise;
    }

    /**
     * @param $query
     */
    public function singleSyncRequest($query)
    {
        $this->requestPromise = $this->requestClient->requestAsync($this->requestMethod, $this->baseUrl.'?'.http_build_query($query));
        $this->requestPromise->then(
            function (ResponseInterface $res) {
                $tempResult = $res->getBody()->getContents();
                $this->requestResult['data'] = Utils::jsonDecode($tempResult,true);
            },
            function (RequestException $e) {
                $this->requestResult['code'] = $e->getCode();
                $this->requestResult['message'] = $e->getMessage();
            }
        );
    }

    /**
     *
     */
   public function setWait()
   {
       $this->requestPromise->wait();
   }
}